package com.example.crawler.service.impl;

import com.example.crawler.model.HighSchool;
import com.example.crawler.service.CrawlerService;
import com.example.crawler.utils.ExeleUtils;
import com.example.crawler.utils.Spider;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author hjq
 *
 * @Date 02/28/2019 16:49
 *
 * @Description //TODO 爬虫实现
 */
@Service
public class CrawlerServiceImpl implements CrawlerService {


    private String BaseURL = "xuexiaodaquan.com";
    /**
     * 根据城市名来查询
     *
     * @param cityName
     */
    @Override
    public HSSFWorkbook initSchoolExele(String cityName, String type) {
        //初始化类型
        String newUrl ="http://"+cityName+".xuexiaodaquan.com/"+type+"/";
        //获取list
        List<HighSchool> highSchoolList = Spider.getHighSchool(newUrl,type);
        //excel标题
        String[] title = {"名称","地址","邮箱","电话","学校级别","网址","附近站点","公交路线","附近地标"};


         //sheet名
        String sheetName = cityName+"市学校表";
        String[][] content = new String[highSchoolList.size()][title.length];
        for (int i = 0; i < highSchoolList.size(); i++) {
            HighSchool highSchool = highSchoolList.get(i);
            content[i][0] = highSchool.getName();
            content[i][1] = highSchool.getAddress();
            content[i][2] = highSchool.getEmail();
            content[i][3] = highSchool.getPhoneNum();
            content[i][4]= highSchool.getSchoolLavel();
            content[i][5]= highSchool.getUrl();
            content[i][6] = highSchool.getSite();
            content[i][7] = highSchool.getBusRoutes();
            content[i][8] = highSchool.getLandmark();
        }
        return ExeleUtils.getHSSFWorkbook(sheetName,title,content,null);
    }
}
